package main

import (
	"bytes"
	"flag"
	"fmt"
	"time"
)

var period = flag.Duration("period", 1*time.Second, "sleep period")

type person struct {
	Name string
	Age  int
}

type personFlag struct {
	person
}

func (p *person) String() string {
	buf := bytes.Buffer{}
	fmt.Fprintf(&buf, "{%s:%d}", p.Name, p.Age)
	return buf.String()
}
func (p *person) Set(s string) error {
	_, err := fmt.Sscanf(s, "%d%s", &p.Age, &p.Name)
	return err
}
func PersonFlag(name string, value person, usage string) *person {
	p := value
	flag.CommandLine.Var(&p, name, usage)
	return &p
}

var p = PersonFlag("person", person{"zhangsan", 14}, "usage introduction")

func main() {
	flag.Parse()
	fmt.Println("Sleep for %v...", *period)
	time.Sleep(*period)
	fmt.Println("---------------------------")

	fmt.Println(p)
}
